package org.mspring.platform.search.listener;

import java.util.HashMap;
import java.util.Map;

import org.hibernate.event.spi.PostDeleteEvent;
import org.hibernate.event.spi.PostDeleteEventListener;
import org.hibernate.persister.entity.EntityPersister;
import org.mspring.platform.search.task.LuceneDeleteTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @author Gao Youbo
 * @since 2013年8月1日
 */
@Component
public class LuceneDeleteEventListener implements PostDeleteEventListener {

    private static final long serialVersionUID = -7548552766458596266L;

    private static final Logger log = LoggerFactory.getLogger(LuceneDeleteEventListener.class);

    @Autowired
    private LuceneDeleteTask luceneDeleteTask;

    @Override
    public void onPostDelete(PostDeleteEvent event) {
        Map<Object, Object> context = new HashMap<>();
        context.put("searchable", event.getEntity());
        try {
            luceneDeleteTask.doAsyncTask(context);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    @Override
    public boolean requiresPostCommitHanding(EntityPersister arg0) {
        return false;
    }

}
